package com.guw.TestThread;

import java.util.ArrayList;
import java.util.List;

//List是线程不安全的
public class TestSyn3 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()-> {
                //锁在这里,不锁每次不到10000,锁了笔记稳定能到10000
                synchronized (list) {
                    list.add(Thread.currentThread().getName());
                }
            }
            ).start();
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}
